package com.watermeter.android.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.watermeter.android.R;
import com.watermeter.android.entry.DownRoute;
import com.watermeter.android.entry.UpRoute;
import com.watermeter.android.utils.UConstants;
import com.watermeter.android.utils.Util;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * 录入抄表
 *
 * @author -zouqh
 */
public class LurucbActivity extends Activity implements OnClickListener {

    private ImageView close;
    private TextView title;
    private TextView save, userName, code, syds, dyyl, address, cbxh;
    private EditText dyds, tbsl;
    private int postion;
    private DownRoute downRoute;
    private Button up, next;
    private int allToal;
    private LinearLayout layout;
    public DbManager.DaoConfig daoConfig;
    private String changeTp;//修改前的退补数量
    private String changeDs;//修改前的当月读数

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        postion = Integer.parseInt(getIntent().getStringExtra("id"));
        Log.i("postion------------",postion+"");
        setContentView(R.layout.luru_cb_activity);
        close = (ImageView) findViewById(R.id.close);
        title = (TextView) findViewById(R.id.title_text);
        save = (TextView) findViewById(R.id.ritgh_text);

        userName = (TextView) findViewById(R.id.userName);
        code = (TextView) findViewById(R.id.code);
        syds = (TextView) findViewById(R.id.syds);
        dyyl = (TextView) findViewById(R.id.dyyl);
        address = (TextView) findViewById(R.id.address);
        cbxh = (TextView) findViewById(R.id.cbxh);
        dyds = (EditText) findViewById(R.id.dyds);
        tbsl = (EditText) findViewById(R.id.tbsl);
        up = (Button) findViewById(R.id.up);
        next = (Button) findViewById(R.id.next);
        layout = (LinearLayout) findViewById(R.id.layout);
        if (UConstants.ShowTBSL != null && UConstants.ShowTBSL.equals("1")) {
            layout.setVisibility(View.VISIBLE);
        }
        next.setOnClickListener(this);
        up.setOnClickListener(this);
        save.setText("保存");
        title.setText("录入抄表");
        save.setVisibility(View.VISIBLE);
        close.setOnClickListener(this);
        save.setOnClickListener(this);
        allToal = RouteItemActivity.activity.downRoutelist1.size();
        setValue();
//        daoConfig = new DbManager.DaoConfig();


    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        Intent intent;
        switch (v.getId()) {
            case R.id.close:
                finish();
                break;
            case R.id.ritgh_text:
                int d = Integer.parseInt(dyds.getText().toString().trim());
                if (d > 0) {
                    UpRoute(dyds.getText().toString().trim(), tbsl.getText().toString().trim());
                } else {
                    Toast.makeText(this, "请填写本月读数!", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.next:
                if (postion < allToal-1) {
                    String string=dyds.getText().toString().trim();
                    String string1=tbsl.getText().toString().trim();
                    if (Util.NoNull(string)){
                        if (!changeDs.equals(string)||!changeTp.equals(string1)){
                            UpRoute(string, string1);
                        }

                    }
                    postion++;
                    setValue();
                } else {
                    Toast.makeText(this, "已经到最后一条了！", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.up:

                if (postion > 0) {
                    String string=dyds.getText().toString().trim();
                    String string1=tbsl.getText().toString().trim();
                    if (Util.NoNull(string)){
                        if (!changeDs.equals(string)||!changeTp.equals(string1)){
                            UpRoute(string, string1);
                        }

                    }

                    postion--;
                    setValue();
                } else {
                    Toast.makeText(this, "没有上一条了！", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                break;
        }
    }

    /**
     * 设置值
     */
    private void setValue() {

        downRoute = RouteItemActivity.activity.downRoutelist1.get(postion);
        if (downRoute != null) {
            int a = downRoute.getSBZD();
            int b = Integer.parseInt(downRoute.getSBQD());
            userName.setText("用户姓名：" + downRoute.getUSERNAME());
            code.setText("用户编号：" + downRoute.getUSERID());
            syds.setText("上月读数：" + downRoute.getSBQD());

            dyyl.setText("当月用量：" + downRoute.getSBQD());
            cbxh.setText("抄表序号：" + downRoute.getCBXH());
            address.setText("详细地址：" + downRoute.getXXDZ());
            changeDs=downRoute.getSBZD()+"";
            if (downRoute.getSBZD()>0){
                dyds.setText(downRoute.getSBZD() + "");
            }else{
                dyds.setText("");
            }
            changeTp=downRoute.getTBSL()+"";
            Log.i("changeDs------------",changeDs);
            Log.i("changeTp------------",changeTp);
            tbsl.setText(changeTp);
            if (a > 0) {
                dyyl.setText("当月用量：" + (a - b));
            } else {
                dyyl.setText("当月用量：0");
            }
        }
    }


    /**
     * 更新
     */
    private void UpRoute(String str1, String str2) {

        DbManager manager = x.getDb(daoConfig);
        try {
            //创建对象，用来生成表
//            downRoute
            int a = Integer.parseInt(str1);//本月读数
            if (!Util.NoNull(str2)){
                str2="0";
            }
            int b = Integer.parseInt(str2);//退补水量
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            Date currentTime = new Date();
            String dateString = dateFormat.format(currentTime);
            RouteItemActivity.activity.downRoutelist1.get(postion).setChange(1);
            RouteItemActivity.activity.downRoutelist1.get(postion).setSBZD(a);
            RouteItemActivity.activity.downRoutelist1.get(postion).setTBSL(b);
            RouteItemActivity.activity.downRoutelist1.get(postion).setCBRQ(dateString);
            downRoute.setSBZD(a);
            downRoute.setTBSL(b);
            downRoute.setChange(1);
            downRoute.setCBRQ(dateString);
            downRoute.setMOTH(RouteItemActivity.activity.moth);
            manager.saveOrUpdate(downRoute);
//            manager.save(info);
            saveUpdate();

        } catch (DbException e) {
            e.printStackTrace();
        }
    }


    /**
     * 保存路线数据
     */
    private void saveUpdate() {

        DbManager manager = x.getDb(daoConfig);
        try {
            //创建对象，用来生成表
            UpRoute upRoute = new UpRoute();
            upRoute.setTBSL(downRoute.getTBSL());
            upRoute.setSBZD(downRoute.getSBZD());
            upRoute.setCBRQ(downRoute.getCBRQ());
            upRoute.setChange(downRoute.getChange());
            upRoute.setId(downRoute.getId());
            upRoute.setUSERID(downRoute.getUSERID());
            upRoute.setUpdate(downRoute.getUpdate());
            upRoute.setSBQD(downRoute.getSBQD());
            upRoute.setMOTH(RouteItemActivity.activity.moth);
//          manager.saveOrUpdate(upRoute);
            RouteItemActivity.activity.flag = 1;
            List<UpRoute> u = manager.selector(UpRoute.class).where("id","=",downRoute.getId()).findAll();
           if(u!=null&&u.size()>0){
               System.out.println("up----" + u.size());

               for (int i=0;i<u.size();i++){
                   upRoute.setUpid(u.get(i).getUpid());
                   manager.saveOrUpdate(upRoute);
               }
           }else{
               manager.saveOrUpdate(upRoute);
           }
            Toast.makeText(this, "保存成功！", 10).show();

        } catch (DbException e) {
            e.printStackTrace();
        }
    }

}
